import 'package:flutter/material.dart';
import 'package:ft_reader/beans/banner_item_bean.dart';
import 'package:ft_reader/blocs/banner_bloc.dart';
import 'package:ft_reader/widgets/banner_widget.dart';
import 'package:ft_reader/beans/store_sex_bean.dart';
import 'package:ft_reader/widgets/store_item.dart';
import 'package:ft_reader/blocs/store_sex_bloc.dart';
import 'package:ft_reader/widgets/reload_widget.dart';
import 'package:ft_reader/widgets/loading_widget.dart';


class SexPage extends StatefulWidget {

  final String sex;

  const SexPage({Key key, this.sex}) : super(key: key);

  @override
  _SexPage createState() => _SexPage();

}

class _SexPage extends State<SexPage>
  with AutomaticKeepAliveClientMixin{

  BannerBloc _bannerBloc = new BannerBloc();

  StoreSexBloc _storeSexBloc = new StoreSexBloc();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _bannerBloc.fetchBannerItems(widget.sex);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      child: StreamBuilder(
        builder: (BuildContext context, AsyncSnapshot<List<BannerItemBean>> banner) {
          if (banner.hasData) {
            _storeSexBloc.fetchStoreSex(widget.sex);
            return RefreshIndicator(
              displacement: 0,
              color: Theme.of(context).primaryColor,
              child: StreamBuilder(
                builder: (BuildContext context,
                  AsyncSnapshot<StoreSexBean> content) {
                  if (content.hasData) {
                    return ListView.builder(
                        itemBuilder: (BuildContext context, int index) {
                          if (index == 0) {
                            return Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: <Widget>[
                                bannerWidget(context, banner.data),
                              ],
                            );
                          } else {
                            return storeItem(context,
                                content.data.data[index].category,
                                content.data.data[index].books);
                          }
                        },
                      itemCount: content.data.data.length,
                    );
                  } else if (content.connectionState == ConnectionState.active &&
                    content.data == null) {
                    return ReloadWidget(refreshCallback: _refresh,);
                  } else {
                    return LoadingWidget(color: Theme.of(context).primaryColor);
                  }
                },
                stream: _storeSexBloc.storeSexData,
              ),
              onRefresh: _refresh,
            );
          } else if (banner.connectionState == ConnectionState.active &&
              banner.data == null) {
            return ReloadWidget(refreshCallback: _refresh,);
          } else {
            return LoadingWidget(color: Theme.of(context).primaryColor);
          }
        },
        stream: _bannerBloc.bannerItems,
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _bannerBloc.dispose();
  }

  Future<void> _refresh() async {
    _bannerBloc.fetchBannerItems(widget.sex);
  }

}